package com._51doit.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @Date 2021/8/24
 * @Created by HANGGE
 * @Description TODO
 */
public class Test01 {
    public static void main(String[] args) throws  Exception {
        Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db_doit26", "root", "root");
         // 停止SQL的自动提交功能
        //#######################开启事务#########################
        conn.setAutoCommit(false);

        // sql1 lny -1000
        PreparedStatement ps1 = conn.prepareStatement("update tb_account set money = money-? where id = ?");
        ps1.setDouble(1,1000d);
        ps1.setInt(2,1);
        ps1.executeUpdate();
        // 有异常
        System.out.println(1/0);
        // sql2  fj+1000
        PreparedStatement ps2 = conn.prepareStatement("update tb_account set money = money+? where id = ?");
        ps2.setDouble(1,1000d);
        ps2.setInt(2,2);
        ps2.executeUpdate();
        // 手动提交事务
        conn.commit();
        //#######################提交事务########################

        ps1.close();
        ps2.close();
        conn.close();



    }

}
